<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      :before-close="close"
      :title="title"
      width="800px"
      :top="'10vh'"
    >
      <el-form ref="form" :model="form" size="small" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item
              label="姓名"
              prop="xm"
              :rules="[{ required: true, message: '请输入值班人员姓名', trigger: 'blur' }]"
              style="width: 100%"
            >
              <el-input v-model="form.xm" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="工号"
              prop="gh"
              :rules="[{ required: true, message: '请输入值班人员工号', trigger: 'blur' }]"
              style="width: 100%"
            >
              <el-input v-model="form.gh" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="人员性质"
              prop="zblx"
              :rules="[{ required: true, message: '请选择人员性质', trigger: 'change' }]"
              style="width: 100%"
            >
              <el-select v-model="form.zblx" placeholder="请选择人员性质" style="width: 200px">
                <el-option
                  v-for="item in dict.zb_type"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="值班日期"
              prop="rq"
              :rules="[{ required: true, message: '请选择人员性质', trigger: 'change' }]"
              style="width: 100%"
            >
              <el-date-picker
                v-model="form.rq"
                type="date"
                format="yyyy/MM/dd"
                value-format="yyyyMMdd"
                placeholder="选择日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否带班领导">
              <el-radio-group v-model="form.dbld">
                <el-radio
                  v-for="item in status"
                  :key="item.id"
                  :label="item.value"
                >{{ item.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="排序"
              prop="xh"
              style="width: 100%"
            >
              <el-input-number v-model="form.xh" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="close">取消</el-button>
        <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { saveObject, updateObject } from '@/api/zbap/index'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  name: 'ManagerResourcesEntity',
  dicts: ['zb_type'],
  data() {
    return {
      fileList: [],
      dialogFormVisible: false,
      baseUrl: process.env.VUE_APP_BASE_API,
      title: '资源管理',
      loading: false,
      depts: [],
      status: [{ id: 0, value: false, label: '否' },
        { id: 1, value: true, label: '是' }],
      form: {
        id: null,
        xm: '',
        zblx: '',
        gh: '',
        dbld: false,
        rq: '',
        xh: 1
      },
      queryData: null
    }
  },
  methods: {
    openEntity(data) {
      if (undefined !== data && undefined !== data.id && data.id !== null) {
        this.form = Object.assign({}, data)
        this.title = '修改资源信息'
        this.dialogFormVisible = true
      } else {
        this.title = '新增资源信息'
        this.dialogFormVisible = true
      }
    },
    save(formName) {
      this.loading = true
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.id !== null && undefined !== this.form.id) {
            updateObject(this.form).then(res => {
              this.$message.success('更新成功！')
              this.close()
            }).catch(() => {
              this.loading = false
              this.$message.error('更新失败！')
            })
          } else {
            saveObject(this.form).then(res => {
              this.$message.success('保存成功！')
              this.close()
              this.loading = false
            }).catch(() => {
              this.loading = false
              this.$message.error('保存失败！')
            })
          }
        } else {
          this.loading = false
          console.log('error submit!!')
          return false
        }
      })
    },
    close() {
      this.loading = false
      this.fileList = []
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.$refs['form'].resetFields()
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    }
  }
}
</script>

<style scoped>
.schoolBottomImage {
  width: 100%;
  height: 250px;
  border: 1px dashed #9b9b9b;
  border-radius: 4px;
  text-align: center;
  line-height: 250px;
  font-size: 34px;
}
.school-bak-image {
  width: 100%;
  height: 100%;
}
</style>
